import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/Home.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    component: Home,
    redirect: "/main",
    children: [
      {
        path:"main",
        component:()=>import("../views/layout/Main.vue"),
        meta: { requiresAuth: true }
      },
      {
        path: "wedDress",
        component: ()=>import("../views/layout/WedDress.vue"),
        meta: { requiresAuth: true }
      },
      {
        path: "wedParty", // 父路由路径
        component: ()=>import("../views/layout/WedParty.vue"),
        meta: { requiresAuth: true }
      },
    {
      path: "/Review",
      component: ()=>import("../views/layout/WedParty/Review.vue")
    },
    {
      path: "/Hot",
      component: ()=>import("../views/layout/WedParty/Hot.vue")
    },
    {
      path:"/viewReview:hotelId",
      component: ()=>import("../views/layout/WedParty/viewReview.vue")
    },
    {
      path:"/Reserve:hotelId",
      component: ()=>import("../views/layout/WedParty/Reserve.vue")
    },
    {
      path:"/hotBanHall:hotelId",
      component: ()=>import("../views/layout/WedParty/hotBanHall.vue")
    },
    {
      path: "us",
      component: ()=>import("../views/layout/Us.vue"),
      meta: { requiresAuth: true }
    }
    ]
  },
  {
    path:"/searchResult/:result/:num",
    name:"search",
    component:()=>import("../views/layout/searchResult.vue")
  },
  {
    path:"/strategy",
    component:()=>import("../views/layout/strategy.vue")
  },
  {
    path:"/wedCar",
    component:()=>import("../views/layout/WedCar.vue")
  },
  {
    path: "/login",
    component: ()=>import("../views/Login.vue"),
    meta: { requiresAuth: false }
  },
  {
    path:"/invitation",
    component:()=>import("@/views/layout/Invitation.vue"),
    redirect:"/invitation/allInvitation",
    children:[
      {
        path:"allInvitation",
        component:()=>import("../views/layout/AllInvitation.vue")
        
      },
      {
        path:"free",
        component:()=>import("../views/layout/Free.vue")
      },
      {
        path:"plus",
        component:()=>import("../views/layout/Plus.vue")
      },
      {
        path:"production",
        component:()=>import("../views/layout/Production.vue")
      }
    ],
    meta: { requiresAuth: true }
  },
  {
        path:"/share",
        component:()=>import("../views/layout/Share.vue")
  },
  {
    path:"/setting",
    component:()=>import("../views/layout/Setting.vue"),
    meta: { requiresAuth: true }
  },
  //配置后台管理主网址
  {
    path: "/admin",
    component: ()=>import("../views/admin/admin.vue"),
    children: [
      {
        path:"adminList",
        component:()=>import("../views/admin/adminList.vue")
      }
    ],
    meta: { requiresAuth: true }
  },
  //人气商家排行榜页面
  {
    path:"/popular",
    component:()=>import("../views/layout/Shops/Popular.vue")
  },
  {
    path:"/shop",
    component:()=>import("../views/layout/Shops/shop.vue")
  },
  {
    path:"/photo",
    component:()=>import("../views/layout/Shops/Photo.vue")
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  // 判断当前路由是否需要登录
  if (to.meta.requiresAuth) {
    try {
      const userStr = localStorage.getItem("user");
      if (userStr) {
        const user = JSON.parse(userStr);
        if (user && user.token) {
          return next();
        }
      }
      next("/login");
    } catch (error) {
      console.error("路由守卫错误:", error);
      next("/login");
    }
  } else {
    // 不需要登录的路由直接放行
    next();
  }
});
export default router
    